tags:

views:

120

answers:

4

Now i have a problem below:

i've added a div on my page to forbid user to click buttons, links or fields when user click the DOWNLOAD button. So i need to remove this div when the IE file download prompt pops up or when user clicks "save", "save as" or "cancel" on it.

How can I reach this?

IE only considered please.

+1  A: 

I don't think there's a callback for that; definitely not in all browsers.

Also, not every browser pops up a prompt - and that depends on user-specific browser configuration; some may just start downloading in the background.

I'd suggest to remove the DIV after some interval (which you'll have to guess depending on the time it usually takes to show the download box), although it's not a very clean workaround.

Piskvor
+2  A: 

No, the browser file handling dialogs can't be detected from javascript - this is by design. Even if you limit yourself to IE, it is not possible, and other browsers handle file downloads in completely different ways anyway.

http://groups.google.co.uk/group/comp.lang.javascript/browse_thread/thread/9441cce4f8fdf20b/6f4fcc7796e03f1d

If you explain why you're trying to do this, there may be a different approach which suits you better.

Colin Pickard
A: 

i add an onblur event to the father window like this:

window.onblur = function (){window.location.reload();}

so when it focus out that is to say when i click the one of the three buttons, the div will be removed.

but if the download request is very slow, at the moment user switches to other window and back the download request will interrupt。

so i need a new solution, anybody could help me?

pat.inside
So your javascript triggers a download, but it takes a while to execute?
Colin Pickard
+5  A: 

There actually is a way that you can make a good guess that the browser is handling a file download. I asked this same question and TJ Crowder had a great idea:

http://stackoverflow.com/questions/2064882/what-are-techniques-to-get-around-the-ie-file-download-security-rules

I'm actually using the idea now and it works perfectly. The trick is to have the page send a "nonce" parameter back with a random string of characters in it. The page then starts polling document.cookie every 100 milliseconds or so, checking to see whether that string of characters is in the cookie yet.

The server, in turn, sets a chosen cookie (doesn't really matter what it's called) to the value sent by the form in the "nonce" parameter. It then ships out the file download as it normally would.

When the HTTP response gets back to the browser, the cookie will be set. The Javascript that's polling for the cookie value will see that, and it will then know that the HTTP response is being processed. Now, it won't know of course that the user didn't hit "Cancel" on the file download.

If the server decides that the original request is in error (say, if the file download involves a form and the user supplied bad input or missed a field), then it won't set the cookie and can just respond with HTML for the error (or whatever).

Pointy
interesting idea
Colin Pickard
nice idea! i'll do it.
pat.inside
@pat.inside then mark the answer correct lol!
Thqr
@Ozaki im sorry that long time been here. It works well!
pat.inside